package com.edu.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.edu.entity.Comments;
import com.edu.entity.Comments2;
import com.edu.service.impl.CommentService2Impl;
import com.edu.service.impl.CommentServiceImpl;
import com.edu.util.PageData;
import com.google.gson.Gson;

/**
 * Servlet implementation class CommentsController
 */
@WebServlet("/CommentsController")
public class CommentsController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	CommentServiceImpl service=new CommentServiceImpl();
	CommentService2Impl service2=new CommentService2Impl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CommentsController() {
        super();
        
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String op=request.getParameter("op");
		      //添加一级评论
			if ("addcomments".equals(op)) {
				addcomments(request,response);
				//查询所有功能
			}else if ("queryComments".equals(op)) {
				queryComments(request,response);	
				//分页查询
			}else if ("queryLikeByPage".equals(op)) {
				queryLikeByPage(request,response);
				//显示默认页码和显示条数
				//添加二级评论
			}else if("addcomments2".equals(op)) {
				addcomments2(request,response);	
				//查询二级评论
			}else if ("queryComments2".equals(op)) {
				queryComments2(request,response);
				//根据一级评论ID查询二级评论
			}else if ("getcoms2".equals(op)) {
				getcoms2(request,response);
				//根据一级评论ID查询二级评论总数
			}else if ("queryComments2_count".equals(op)) {
				queryComments2_count(request,response);
				//根据时间显示评论
			}else if ("queryLikeTime".equals(op)) {
				queryLikeTime(request,response);
			}else if ("queryComSum".equals(op)) {
				queryComSum(request,response);
			}
	}

	private void queryComSum(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		String artcleId = request.getParameter("articleId");
		int sum = Integer.parseInt(artcleId);
		int comSum = service.queryComSum(sum);
		response.getWriter().print(comSum);
		response.getWriter().close();
	}

	private void queryLikeTime(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		ArrayList<Comments> list = service.getQueryByTime();
		Gson gson=new Gson();
		String jsonstring=gson.toJson(list);
		response.getWriter().print(jsonstring);
		response.getWriter().close();	
	}

	private void queryComments2_count(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		String sum=request.getParameter("comid");
		int count=service2.getCom2Count(Integer.valueOf(sum));
		response.getWriter().print(count);
		response.getWriter().close();
		
	}

	private void getcoms2(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		String comid=request.getParameter("comid");
		ArrayList<Comments2>list=service2.getByComId(Integer.valueOf(comid));
		System.out.println(list.size());
		if (list.size() >0) {
			Gson gson = new Gson();
			String conJson=gson.toJson(list);
			response.getWriter().print(conJson);
			response.getWriter().close();
		} 
		
	}

	private void queryComments2(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		ArrayList<Comments2> list = service2.getQueryAll();
		request.setAttribute("comment2", list);	
	}

	private void addcomments2(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		String josnStr = request.getParameter("comments2");
		Gson gson=new Gson();
		Comments2 comments2=gson.fromJson(josnStr, Comments2.class);
		boolean flag = service2.getAddCom(comments2);
		String json = gson.toJson(flag);
		response.getWriter().print(json);
		response.getWriter().close();
		
	}

	private void queryLikeByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub1
		int pageNO=1;//默认值
		int pageSize=3;
		int articleId=0;//如果没有传递,默认不显示
		//如果前台有指定值就使用前台的数据,否则就按默认显示
		if (request.getParameter("pageNO")!=null) {
			pageNO= Integer.parseInt(request.getParameter("pageNO"));
		}
		if (request.getParameter("pageSize")!=null) {
			pageSize= Integer.parseInt(request.getParameter("pageSize"));
		}
		if (request.getParameter("articleId")!=null) {
			articleId= Integer.parseInt(request.getParameter("articleId"));
		}
		System.out.println(pageNO+","+pageSize+","+articleId);
		PageData page = service.getQueryLikePage(pageNO, pageSize, articleId);
		//通过ajax返回json数据
		Gson gson=new Gson();
		String json = gson.toJson(page);
		response.getWriter().print(json);
		response.getWriter().close();
		
	}

	private void queryComments(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		List<Comments> list = (List<Comments>)service.getQueryAll();
		Gson gson=new Gson();
		String jsonstring=gson.toJson(list);
		response.getWriter().print(jsonstring);
		response.getWriter().close();		
	}

	private void addcomments(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		String josnStr = request.getParameter("comments");
		Gson gson=new Gson();
		Comments comments=gson.fromJson(josnStr, Comments.class);
		boolean flag = service.getAddCom(comments);
		String json = gson.toJson(flag);
		response.getWriter().print(json);
		response.getWriter().close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
